//
//  mainNavViewController.swift
//  ledian
//
//  Created by 三秒文化 on 16/2/27.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit

@objc
protocol LDRootNavDelegate:UINavigationControllerDelegate {
    
    optional func navWillPop()
}



class LDRootNavVC: UINavigationController{//UINavigationController

    private var  tapBackColure:(()->Void)?
    private var tapPushClosure:(()->Void)?
    private var barLeft:UIBarButtonItem?
    
    var popdelegate: LDRootNavDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        interactivePopGestureRecognizer!.enabled = true
        interactivePopGestureRecognizer!.delegate = nil
        
        navigationBar.barTintColor = LDGolbalColor.Green
        navigationBar.tintColor = UIColor.whiteColor()
        navigationBar.translucent = false
        let image = UIImage.setImageWithColor(LDGolbalColor.Green.colorWithAlphaComponent(1))
//        let shdowimg = UIImage.setImageWithColor(LDGolbalColor.Green.colorWithAlphaComponent(1))
        
//        navigationBar.shadowImage = UIImage()
        print(image.description)
        navigationBar.setBackgroundImage(image, forBarMetrics: UIBarMetrics.Compact)
        
        navigationBar.titleTextAttributes = [NSFontAttributeName : LDFont.Font16,NSForegroundColorAttributeName:LDGolbalColor.White]
        
        //左bar图
        
        barLeft = UIBarButtonItem(image: UIImage(named:"navBack.png"), style: UIBarButtonItemStyle.Plain, target: self, action:#selector(self.navBack))
        
    }
    
    override  func pushViewController(viewController: UIViewController, animated: Bool) {
        
        if self.childViewControllers.count > 0 {
        
            viewController.navigationItem.leftBarButtonItem = barLeft
        }
        
        if tapPushClosure != nil {
            tapPushClosure!()
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
//    override func pushViewController(viewController: UIViewController!, animated: Bool, complete block: ((Bool) -> Void)!) {
//        
//        
//        if tapPushClosure != nil {
//            tapPushClosure!()
//        }
//        
//    }
    
    //拦截pop事件
    func navBack(){
        
        if tapBackColure != nil {
        
            NSOperationQueue.mainQueue().addOperationWithBlock({ [weak self] in
                
                self?.tapBackColure!()
                self?.popViewControllerAnimated(true)
                
            })
           
            
        } else {
        
            popViewControllerAnimated(true)
        }

        if popdelegate != nil {
            
            popdelegate?.navWillPop!()
        }
        
        
        
    }
    
    //返回回调
    func tapBackNavClosure(colusre:(()->Void)) {
        
        tapBackColure = colusre
//        popViewControllerAnimated(true)
    }
    //push回调
    func tapPushNavClosure(colusre:(()->Void)?) {
        
        tapPushClosure = colusre
    }
    
    override  func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }
  
}

extension LDRootNavVC:UINavigationControllerDelegate{


}
